package shootingtactics.client.core.enemy;

import shootingtactics.client.core.Constant;
import shootingtactics.client.core.bullet.ExplotionBullet;
import shootingtactics.client.core.bullet.HomingBullet;

import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;
import com.google.gwt.user.client.Random;

public class LoopEnemy extends BaseEnemy{

    public LoopEnemy(double posX, double posY, String drawType, CssColor drawColor, Context2d enemyContext, Context2d bulletContext){
        super(posX, posY, drawType, drawColor, enemyContext);
        this.bulletContext = bulletContext;
        setSpeed();
        health = 100;
    }

    private void setSpeed(){
        baseSpeed = Random.nextInt(20) + 10;
        speedX = 0;
        speedY = baseSpeed;
    }

    @Override
    public void updateSpeed(){
        super.updateSpeed();

        if(position.getPositionY() + position.getHeight() / 2 > Constant.CANVAS_HEIGHT){
            speedY = -speedY;
            position.setPosition(position.getPositionX(), Constant.CANVAS_HEIGHT - position.getHeight() / 2);
        }else if(position.getPositionY() - position.getHeight() / 2 < 0){
            speedY = -speedY;
            position.setPosition(position.getPositionX(), 0 + position.getHeight() / 2);
        }

        addBullet();
    }

    public void addBullet(){
        if(System.currentTimeMillis() - bulletTime > 100){
            bulletList.add(new HomingBullet(position.getPositionX(), position.getPositionY(), Constant.DRAW_TYPE_RECTANGLE, bulletContext, targetEnemy));
            bulletList.add(new ExplotionBullet(position.getPositionX(), position.getPositionY(), Constant.DRAW_TYPE_RECTANGLE, bulletContext, targetEnemy));
            bulletTime = System.currentTimeMillis();
        }
    }

}
